package com.atguigu.yygh.hosp.controller.api;


import com.atguigu.yygh.hosp.result.hosp.Result;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.util.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

//该类用于开发8个接口
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    HospitalService hospitalService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    ScheduleService scheduleService;

    /**
     * 接口8:删除排班
     */
    @PostMapping("/schedule/remove")
    public Result scheduleRemove(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        scheduleService.remove(map);
        return Result.ok();
    }

    /**
     * 接口7:删除科室
     */
    @PostMapping("/department/remove")
    public Result departmentRemove(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        departmentService.remove(map);
        return Result.ok();
    }


    /**
     * 接口6:查询排班,条件+分页
     */
    @PostMapping("/schedule/list")
    public Result scheduleList(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        Page<Schedule> page = scheduleService.selectPage(map);
        return Result.ok(page);
    }


    /**
     * 接口5: 查询科室,条件+分页
     * 需求: 带条件的分页查询科室,返回Page对象,赋值给Result中的data属性
     */
    @PostMapping("department/list")
    public Result departmentList(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //查询科室列表(条件加分页),返回Page对象
        Page<Department> page = departmentService.selectPage(map);
        return Result.ok(page);
    }


    /**
     * 接口2:上传排班
     */
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        scheduleService.save(map);
        return Result.ok();
    }



    /**
     * 接口2:上传科室
     */

    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        departmentService.save(map);
        return Result.ok();
    }




    /**
     *上传医院接口的需求
     *(医院端传过来的参数上传到mongodb中)
     *1.签名校验,校验成功,才可以上传,否则直接抛出异常
     *    医院端传递的一个参数sign(已经经过md5加密的签名)
     *    从尚医通平台的医院设置表中根据hoscode查询医院设置表中的signKey
     *    该值没有加密,在进行校验时,将医院设置表中的签名进行md5加密之后和医院端传递的签名进行比较即可
     *2.医院端传递的医院信息map,转成Hospital对象
     *3.为该医院的status赋予默认值,医院的上下线状态默认=1(已上线)
     *4.为医院端传递的logoData,将字符串中的所有空格替换成+
     *    logoData.replaceAll(" ","+");
     *5.判断该医院在mongdb中是否存在,如果不存在,直接添加(每个医院都有创建时间和最后修改时间)
     *   根据hoscode医院编号(唯一性)去mongodb中查询一下,如果没有查询到,说名不存在,执行添加操作
     *6.如果该医院存在,最终执行更新操作(根据主键更新,createTime字段保持不变,updateTime要更新)
     */
    //上传医院
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //String hoscode = request.getParameter("hoscode");
        //获取请求中所有的参数,这里的value是string数组,其实各个字段的值都是单一的,没有数组,数组中也就只有一个值
       /**
        获取请求中所有的参数,这里的value是string数组,其实各个字段的值都是单一的,没有数组,数组中也就只有一个值
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,String> map=new HashMap<>();
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            String key = entry.getKey();
            String[] value = entry.getValue();
            map.put(key,value[0]);
        }
       */

        /**
          调用HttpRequestHelPer里面的switchMap
           8个接口的接收参数的方式,都这么写
         */
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        hospitalService.saveHospital(map);
        return Result.ok();//利用SUCCESS(200,"成功")枚举值,创建了一个默认的Result对象(code=200,message:成功,data:null)
    }

    //第四个接口,查询
    //根据hoscode从mongodb中查询医院对象
    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request){
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        Hospital hospital=hospitalService.show(map);
        return Result.ok(hospital);//{code:200,message:'成功',data:hospital}

    }
}
